package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.qo.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionService implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void saveOrUpdate(Permission permission) {
        if(permission.getId() != null){
            permissionMapper.updateByPrimaryKey(permission);
        } else {
            permissionMapper.insert(permission);
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            permissionMapper.deleteByPrimaryKey(id);

            roleMapper.deleteRelationRoleAndPermissionByPermissionId(id);
        }
    }

    @Override
    public Permission getPermissionById(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> getAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> selectByQuery(QueryObject queryObject) {
//        int totalCount = permissionMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Permission> permissions = permissionMapper.selectByQuery(queryObject);
        return new PageInfo<>(permissions);
    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void selectAllExpressions() {
        List<String> expressions = permissionMapper.selectAllExpressions();
        //拿到controller贴的对象Mapper
        Map<String, Object> beansWithAnnotation = ctx.getBeansWithAnnotation(Controller.class);
        //System.out.println(beansWithAnnotation);
        //拿到类的对象们
        Collection<Object> vals = beansWithAnnotation.values();
        for (Object value : vals) {
            //通过对象到字节码  再找到所有的方法
            Method[] declaredMethods = value.getClass().getSuperclass().getDeclaredMethods();
            for (Method method : declaredMethods) {
                //遍历方法,拿到方法上贴注解的注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
//                String[] val = annotation.value();
//                System.out.println(val);
                if(annotation != null){
                    //注解的值 是数组对象

                    String[] val = annotation.value();
                    if(!expressions.contains(val[1])){
                        Permission permission = new Permission();
                        permission.setName(val[0]);
                        permission.setExpression(val[1]);
                        permissionMapper.insert(permission);
                    }

                }
            }
        }
    }
}
